import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";

const useUpdateStaff = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const updateStaff = async (values, callback) => {
        state.loading = true;
        if (values.osName) values.pyName = pinyin(values.osName.trim(), { toneType: "none", nonZh: "consecutive" });
        if (values.osPhon) values.osPhon = values.osPhon?.trim();
        if (values.bmSala) values.bmSala = parseFloat(values.bmSala);
        if (values.daSala) values.daSala = parseFloat(values.daSala);
        if (values.fuDays) values.fuDays = parseInt(values.fuDays);
        if (values.ssBase) values.ssBase = parseInt(values.ssBase);
        let res = await graphqlFetch({
            query,
            variables: values,
        });
        callback && callback(res);
        state.loading = false;
        return res;
    };

    return { ...state, updateStaff };
};

let query = compress(`mutation MyMutation(
    $osUuid: UUID!
    $osType: String
    $osName: String
    $pyName: String
    $osRoge: String
    $osPhon: String
    $osNati: String
    $osIdnu: String
    $osBirt: Date
    $opUuid: UUID
    $deInfo: JSON
    $bmSala: BigFloat
    $cpInfo: JSON
    $cpPosi: String
    $daSala: BigFloat
    $ecInfo: JSON
    $fuDays: Int
    $osAddr: String
    $osCity: String
    $osDist: String
    $osFrom: String
    $osPrin: String
    $osZoja: JSON
    $ssBase: Int
  ) {
    updateOrganizationStaffByOsUuid(
      input: {
        osUuid: $osUuid
        organizationStaffPatch: {
          osName: $osName
          osType: $osType
          pyName: $pyName
          osRoge: $osRoge
          osPhon: $osPhon
          osNati: $osNati
          osIdnu: $osIdnu
          osBirt: $osBirt
          opUuid: $opUuid
          deInfo: $deInfo
          bmSala: $bmSala
          cpInfo: $cpInfo
          cpPosi: $cpPosi
          daSala: $daSala
          ecInfo: $ecInfo
          fuDays: $fuDays
          osAddr: $osAddr
          osCity: $osCity
          osDist: $osDist
          osFrom: $osFrom
          osPrin: $osPrin
          osZoja: $osZoja
          ssBase: $ssBase
        }
      }
    ) {
      clientMutationId
    }
}`);

export default useUpdateStaff;
